package io.renren.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.renren.annotation.LoginUser;
import io.renren.common.service.impl.CrudServiceImpl;
import io.renren.common.utils.Result;
import io.renren.dao.FoodmxDao;
import io.renren.dto.AuxiliaryMaterialsmxDTO;
import io.renren.dto.FoodmxDTO;
import io.renren.entity.FoodmxEntity;
import io.renren.entity.UserEntity;
import io.renren.service.AuxiliaryMaterialsmxService;
import io.renren.service.FoodmxService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 辅材库明细表
 *
 * @author 罗胜平 781757616@qq.com
 * @since 1.0.0 2019-11-22
 */
@Service
public class FoodmxServiceImpl extends CrudServiceImpl<FoodmxDao, FoodmxEntity, FoodmxDTO> implements FoodmxService {
    @Autowired
    private AuxiliaryMaterialsmxService auxiliaryMaterialsmxService;
    @Autowired
    private FoodmxService foodmxService;
    @Autowired
    private FoodmxDao foodmxDao;
    @Override
    public QueryWrapper<FoodmxEntity> getWrapper(Map<String, Object> params){
        String id = (String)params.get("id");

        QueryWrapper<FoodmxEntity> wrapper = new QueryWrapper<>();
        wrapper.eq(StringUtils.isNotBlank(id), "id", id);

        return wrapper;
    }
    @Override
    public void saveFoodmx(Long oder, Long canteenId) {

        //获取当前时间戳  创建批次号
        Long timeStamp = System.currentTimeMillis();
        SimpleDateFormat sdf=new SimpleDateFormat("yyyyMMdd");
        // 时间戳转换成时间
        String time = sdf.format(new Date(Long.parseLong(String.valueOf(timeStamp))));      // 时间戳转换成时间

        //根据订单号查询详情 并遍历，判断空有没有数据
        List<FoodmxDTO> list = foodmxService.selectFoodmx(time);
        int str =list.size()+1;
        String batchNumber =  time+"00" +str;

        List<AuxiliaryMaterialsmxDTO> stockOrdermxDTOList = auxiliaryMaterialsmxService.seletMx(oder);
        for (AuxiliaryMaterialsmxDTO materialsmxDTO:stockOrdermxDTOList) {
            FoodmxDTO foodmxDTO = new FoodmxDTO();
            foodmxDTO.setOperatorTime(new Date());
            foodmxDTO.setAuxiliaryMaterialsmxId(materialsmxDTO.getAuxiliaryMaterialsmxId());
            foodmxDTO.setFoodId(materialsmxDTO.getFoodId());
            foodmxDTO.setFoodNum(materialsmxDTO.getArrivalNum());
            foodmxDTO.setUnitPrice(materialsmxDTO.getUnitPrice());
            foodmxDTO.setBatchNumber(batchNumber);
            foodmxDTO.setCanteenId(canteenId);
            //操作人*
            foodmxService.save(foodmxDTO);
        }
    }

    @Override
    public List<FoodmxDTO> selectFoodmx(String time) {
        List<FoodmxDTO> list = foodmxDao.selectFoodmx( time);
        return list;
    }
    @Override
    public List<FoodmxDTO> selectAllFoodmx(String name,String variety,int sign,Long orderId, @LoginUser UserEntity entity) {
        String foodName = "";
        String variety1 = "";
        if(StringUtils.isNotBlank(name)){
           foodName=" and fd.food_name LIKE '%"+name+"%'";
        }
        if(StringUtils.isNotBlank(variety)){
            variety1 = " and fd.variety = "+variety;
        }
        List<FoodmxDTO> list = foodmxDao.selectAllFoodmx(foodName,variety1, entity.getDeptId());
        for (FoodmxDTO foodmxDTO:list) {
            FoodmxDTO temCollFood = foodmxDao.getTemCollFood(foodmxDTO.getFoodId(), sign, entity.getId(), entity.getDeptId());
            if(null!=temCollFood){
                foodmxDTO.setFlag(1);
                foodmxDTO.setFoodNum(temCollFood.getFoodNum());
                foodmxDTO.setPurchaseNum(temCollFood.getFoodNum());
            }
        }

        return list;
    }

    /**
     * 根据商品id获取当前库中该商品的数量
     * @param foodId
     * @return
     */
    @Override
    public List<FoodmxDTO> selectFoodIdmx(Long foodId) {
        List<FoodmxDTO> list = foodmxDao.selectFoodIdmx(foodId);
        return list;
    }
    /**
     * 删除当前库存
     * @param foodId
     * @return
     */
    @Override
    public void delectFoodIdmx(Long foodId) {
        foodmxDao.delectFoodIdmx(foodId);
    }

    @Override
    public FoodmxDTO getTotaoNum(Long foodId,long canteenId) {
        FoodmxDTO totaoNum = foodmxDao.getTotaoNum(foodId,canteenId);
        return totaoNum;
    }

}